package com.shen.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.shen.commonutils.Result;
import com.shen.exceptionHandler.GuliException;
import com.shen.vod.service.VodService;
import com.shen.vod.util.ConstantPropertiesUtil;
import org.apache.catalina.connector.Response;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.shen.vod.util.initVodClient.initVodClient;


@RestController

@CrossOrigin
@RequestMapping("/eduvod/video")
public class VideoController {
    @Autowired
    private VodService vodService;
    @PostMapping("uploadVideo")
    public Result uploadVideo(MultipartFile file) throws IOException {

        System.out.println("uplaod----");
        //response.setHeader("Access-Control-Allow-Origin", "*");
        String videoId=vodService.uploadVideo(file);
        String filename = file.getOriginalFilename();
        System.out.println(videoId);
        return  Result.ok().data("videoId",videoId).data("filename",filename);
    }
    //根据id删除视频
    @DeleteMapping("removeVideo/{id}")
    public  Result removeVideo(@PathVariable String id){
        try {
            DefaultAcsClient client = initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID,ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();
            request.setVideoIds(id);
            response = client.getAcsResponse(request);
            return Result.ok();
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }

    }
    @DeleteMapping("removeVideos")
    public  Result removeVideos(@RequestParam("videoList") List<String> videoList){
        try {
            System.out.println("进入 VOD delete video");
            System.out.println(" videolist -----");
            System.out.println(videoList);
            DefaultAcsClient client = initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID,ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();
            String videoIds= StringUtils.join(videoList.toArray(), ",");
            System.out.println("videoIds");
            System.out.println(videoIds);
            request.setVideoIds(videoIds);
            response = client.getAcsResponse(request);
            return Result.ok();
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }

    }

    @GetMapping("getPlayAuth/{id}")
    public  Result getPlayAuth(@PathVariable String  id){

        try{
            DefaultAcsClient defaultAcsClient = initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //创建request 和response对象
            GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
            //向resquest里面设置id
            request.setVideoId(id);
            GetVideoPlayAuthResponse response=defaultAcsClient.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return  Result.ok().data("playAuth",playAuth);
        }
        catch (Exception e){
            throw  new GuliException(20001,"获取播放权限失败");

        }

    }

}
